$(function(){

    let Rx_account = /^\d+?\d+$/;  // 学号
    let Rx_userName = /^[\u4e00-\u9fa5]{2,4}$/; //姓名
    let Rx_phone = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;// 手机
    let Rx_email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; // 邮箱

    let OFF_email = false,
        OFF_id = false,

        newEmailVAL = '',
        oldEmailVAL = 'str',// 第一次输入的数据

        newIdVAL = '',
        oldIdVAL = 'str';// 第一次输入的数据

    initPage();

    function initPage() {
        getCollage();
    }
    /**********************************************/

    // 检测 账号
    $('#userName').blur(function(){
        newIdVAL = $(this).val();

        if((newIdVAL != oldIdVAL) && newIdVAL ){// 如果输入不为空
            let ACCOUNT = Rx_account.test(newIdVAL);// 正则 格式
            if( ACCOUNT ){
                $.ajax({
                    url : '/user/check_valid.do',
                    type : 'post',
                    data : {
                        type : 'stuid',
                        str : newIdVAL
                    },
                    dataType : 'json',
                    xhrFields : {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success : function(data){
                        if(data.status ==0){
                            spop_ok( '学号/工号 校验成功', function () {
                                oldIdVAL = newIdVAL;

                                OFF_id = true;
                            } );
                        }else if( data.status == 10 ){
                            spop_waring( data.msg, function () {}, outLoing );
                        }else{
                            spop_er( data.msg, function () {
                                $('#userName').val('');
                                newIdVAL = '';
                                oldIdVAL = 'str';

                                OFF_id = false;
                            },function () {
                                addErrCSS('#userName');
                                improtText('#userName');
                            });
                        }
                    }
                });
            }else{
                spop_er( '学号格式错误', function () {
                    $('#userName').val('');
                    newIdVAL = '';
                    oldIdVAL = 'str';
                },function () {
                    addErrCSS('#userName');
                    improtText('#userName');
                } );
                return false;
            }
        }
    });
    // 检测 email
    $('#email').blur(function(){
        newEmailVAL = $(this).val();

        if( (newEmailVAL != oldEmailVAL) && newEmailVAL ){
            let EMAIL = Rx_email.test(newEmailVAL);// 正则 验证
            if( EMAIL ){
                $.ajax({
                    url : '/user/check_valid.do',
                    type : 'post',
                    data : {
                        type : 'stuemail',
                        str : newEmailVAL
                    },
                    dataType : 'json',
                    xhrFields : {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success : function(data){
                        if(data.status ==0){
                            spop_ok( '邮箱 校验成功', function () {
                                oldEmailVAL = newEmailVAL
                            },function () {
                                OFF_email = true;
                            });
                        }else if( data.status == 10 ){
                            spop_waring(data.msg, function () {}, outLoing );
                        }else{
                            spop_er( data.msg, function () {
                                $('#email').val('');
                                newEmailVAL = '';
                                oldEmailVAL = 'str';
                            },function () {
                                addErrCSS('.stuemail');
                                improtText('.stuemail');

                                OFF_email = false;
                            });
                        }
                    }
                });
            }else{
                spop_er( '邮箱格式错误', function () {
                    $('#email').val('');
                    newEmailVAL = '';
                    oldEmailVAL = 'str';
                },function () {
                    addErrCSS('.stuemail');
                    improtText('.stuemail');
                });
                return false;
            }
        }
    });
    // 注册提交
    $('#zhuCe-ok-btn').on('click',function(){

        let USERNAME = Rx_userName.test( $('#myName').val().trim() ),// 姓名
            PHONE = Rx_phone.test( $('#phone').val().trim() ),// 手机号
            password = $('#passWord').val().trim();

        // 密码
        if( !password ){
            addErrCSS('#passWord');
            improtText('#passWord');
        }
        // 姓名
        if( !USERNAME ){
            addErrCSS('#myName');
            improtText('#myName');
        }
        // 手机
        if( !PHONE ){
            addErrCSS('#phone');
            improtText('#phone');
        }
        // 账号
        if( !OFF_id ){
            addErrCSS('#userName');
            improtText('#userName');
        }
        // 邮箱
        if( !OFF_email ){
            addErrCSS('#email');
            improtText('#email');
        }

        // 如果全部输入正确
        if( USERNAME && PHONE && OFF_id && password && OFF_email ){
            // 提交表单
            submitForm();
        }else{
            spop_er( '信息输入有误' );
        }
    });

    $('#stuinstitute').on('change',function () {
        getSpecialty( $(this).val() );
    });
    $('#profession').on('change',function () {
        getClass( $(this).val() );
    });

    // 提交表单
    function submitForm(){
        $.ajax({
            url: '/user/register.do',
            type: 'post',
            dataType : 'json',
            data : $('#form').serialize(),
            success: function (data) {
                if(data.status == 0 ){
                    spop_ok( '注册成功',function(){},function () {
                        window.location.href = './logon.html';
                    });
                }else{
                    spop_er('注册失败');
                }
            }
        })
    }
    // 获取所有学院信息
    function getCollage() {
        let Pr_collage = new Promise( (resolve,reject)=>{
            $.ajax({
                url : '/institute/list.do',
                type : 'post',
                data : {},
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else{
                        reject(data);
                    }
                }
            });// 获取所有学院信息
        } );
        Pr_collage.then( (data)=>{
            let Box = $('#stuinstitute');
            Box.html('');

            let aCollageInfo = data.data;
            for(let i=0; i<aCollageInfo.length; i++){
                $('<option value="'+ aCollageInfo[i].stuinstitutename +'">'+ aCollageInfo[i].stuinstitutename +'</option>').appendTo(Box);
            }

            getSpecialty( aCollageInfo[0].stuinstitutename);
        }).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }
        } );
    }
    // 获取专业
    function getSpecialty( data, callBack ) {
        console.log(data);
        let Pr_specialty = new Promise( (resolve,reject) => {
            $.ajax({
                url : '/profession/list.do',
                type : 'post',
                data : {
                    stuInstitutename : data
                },
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else {
                        reject(data);
                    }
                }
            });
        } );
        Pr_specialty.then( (data)=>{
            let Box = $('#profession');
            Box.html('');

            let aSpecialty = data.data;
            for(let i=0; i<aSpecialty.length; i++ ){
                $('<option value="'+ aSpecialty[i].professionname +'">'+ aSpecialty[i].professionname +'</option>').appendTo(Box);
            }

            getClass( aSpecialty[0].professionname, callBack );
        } ).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }
        } )
    }
    // 获取所有班级
    function getClass(data, callBack) {
        console.log(data);
        let Pr_class = new Promise( (resolve,reject) => {
            $.ajax({
                url : '/class/list.do',
                type : 'post',
                data : {
                    professionName: data
                },
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if( data.status == 0 ){
                        resolve(data);
                    }else{
                        reject(data);
                    }
                }
            });
        } );// 获取所有班级信息
        Pr_class.then((data)=>{
            console.log(data);
            let Box = $('#stuclass');
            Box.html('');

            let aClassInfo = data.data;
            for(let i=0; i<aClassInfo.length; i++ ){
                $('<option value="'+ aClassInfo[i].classname +'">'+ aClassInfo[i].classname +'</option>').appendTo( Box );
            }

            if( typeof callBack == 'function'){
                callBack();
            }
        }).catch( (data)=>{
            if(data.status == 10){
                spop_waring( data.msg, function () {}, outLoing )
            }
        } );
    }
    // 输入 监听
    function improtText(id){
        let val = $(id).val();
        $(id).bind("input propertychange",function(event){
            if( val != $(this).val() ){
                removeErrCSS(id);
            }else{
                addErrCSS(id);
            }
        });
    }
    // 添加 错误样式
    function addErrCSS(id){
        $(id).parent().addClass('has-error').addClass('has-feedback');
        $(id).parent().find('span').removeClass('off-display');
    }
    // 删除 错误样式
    function removeErrCSS(id){
        $(id).parent().removeClass('has-error').removeClass('has-feedback');
        $(id).parent().find('span').addClass('off-display');
    }
});